﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Nave.Technique
{
    /// <summary>
    /// 动态图集 UGUI
    /// </summary>
    public static class DynamicAtlas
    {
        public readonly static int MAX_ATLAS_SIZE = 2048;

        public readonly static int MAX_CELL_COUNT = 8;

        private static Texture2D s_dynamicTex;

        static SpriteInfo[] s_spriteInfos = null;

        static Dictionary<int, int> s_spriteId2Index = null;

        //开关
        public static bool Opened = false;

        public static void Initlize()
        {
            if (!Opened) return;
            s_spriteId2Index = new Dictionary<int, int>();
            s_spriteInfos = new SpriteInfo[MAX_CELL_COUNT * MAX_CELL_COUNT];
            s_dynamicTex = new Texture2D(MAX_ATLAS_SIZE, MAX_ATLAS_SIZE, TextureFormat.ARGB32, false);
            for (int i = 0; i < MAX_CELL_COUNT * MAX_CELL_COUNT; i++)
                s_spriteInfos[i] = new SpriteInfo();
        }

        public static void Clean()
        {
            if (!Opened) return;
            s_spriteId2Index.Clear();
        }

        public static void RegisImage(Sprite sprite)
        {
            if (!Opened) return;
            var bytes = sprite.texture.GetRawTextureData();
            s_dynamicTex.LoadRawTextureData(bytes);

        }

        public static void Unregist(Sprite sprite)
        {
            if (!Opened) return;

        }

        /// <summary>
        /// 单张sprite数据
        /// </summary>
        public class SpriteInfo
        {
            public int x, y;
            public int width, heigh;
            public int referencNum;
            public bool used;
        }
    }
}

